
# This is an example of a platform defs.mk file. This file should define all
# the necessary targets needed to perform the compilation and linking process.

# First of all, the user can define a set of PHONY targets, e.g.:

.PHONY: g gdb

# In this case, we have defined two phony targets: g and gdb. Both targets
# can be used to run an instance of the GNU's Debugger GDB.

# The next step is to define the remaining compiling and linking flags that
# are going to be used when performing the compilation and linkage. As an 
# example:

# CPPFLAGS+=
CFLAGS+= -Wall -g -mv8 -nostdinc -msoft-float  -fno-exceptions -fno-builtin -fomit-frame-pointer
CXXFLAGS+= -fno-rtti -O2  
# LDFLAGS+=

# Now we can define a set of names of the most common GCC utils, e.g.:

# The PATH enviroment variable should include the complete path to the sparc-elf binaries folder
# otherwise it has to be specified down here.
CROSS:=sparc-elf-

AS:=$(CROSS)as
LD:=$(CROSS)ld
CC:=$(CROSS)gcc
CXX:=$(CROSS)g++
CPP:=$(CROSS)cpp
AR:=$(CROSS)ar
NM:=$(CROSS)nm
OBJCPY:=$(CROSS)objcopy
OBJDUMP:=$(CROSS)objdump
STRIP:=$(CROSS)strip

GDB:=$(CROSS)gdb

LST:=$(TARGET).lst
MAP:=$(TARGET).map
# Now we have to define the default targets for all the types of source files:

.c.o:
	@echo " CC  $<"
	@$(CC) $(CFLAGS) $(CPPFLAGS) -MD -c -o $@ $<

.cpp.o:
	@echo " CXX $<"
	@$(CXX) $(CFLAGS) $(CPPFLAGS) $(CXXFLAGS) -MD -c -o $@ $<

.S.o:
	@echo " CC  $<"
	@$(CC) $(ASMFLAGS) $(CPPFLAGS) -c -o $@ $<


$(LDSCRIPT) : $(LDSCRIPT_IN)
	@$(CPP) $< $(CPPFLAGS) | awk '$$0 !~ /^#/ {print}' > $@
	
# Once defined the default targets, we have to define the main target of the
# application, i.e. $(TARGET):

$(TARGET): $(MOBJ) $(OBJS) $(LDSCRIPT)
	@echo " LD  $@"
# OJO: hay que meter los flags "-msoft-float -mv8" para que tome la libgcc.a correcta
	$(LD) -nostdlib $(LDFLAGS) $(MOBJ) $(OBJS) $(shell $(CC) -msoft-float -mv8 -print-libgcc-file-name) $(LIBGCC) -o $@  > $(MAP)
	@echo "OBJDUMP $@"
	$(OBJDUMP) -d $(TARGET) > $(LST)
	
GRMON_OPTIONS:= -u -uart /dev/ttyUSB2 -c ./grmon_start.bin -port 1234
grmon:
	@echo "***************************************"
	@echo "Iniciando GRMON..."
	@echo "***************************************"
	@echo "wash" > grmon_start.bin
	@echo "lo $(TARGET)" >> grmon_start.bin
#	@echo "gdb" >> grmon_start.bin
	@grmon-eval $(GRMON_OPTIONS) $(TARGET)

# This target below is a static checker for c++ code. The program is GPL and can 
# be downloaded from http://cppcheck.sourceforge.net/.
CPPCHECK:=/opt/cppcheck/cppcheck
cppcheck:	$(SRCS) $(MSRC)
	@echo "checking cpp source files staticly"
	$(CPPCHECK) --xml $(MSRC) $(SRCS) 2> cppcheck.xml


# To finish, we can define the g and gdb target to help us with the debugging
# process of the application.			
g gdb : $(TARGET)
	@echo "**************************************************************"
	@echo "Generating starting commands for gdb in file <.gdbinit> ..."
	@echo "**************************************************************"
	@echo "target extended-remote localhost:1234" > .gdbinit
	@echo "load" >> .gdbinit
	@echo "break main" >> .gdbinit
	$(GDB) $(TARGET)
	
platform-clean :
	rm -rf $(LDSCRIPT)
	rm -f $(MAP)
	rm -f $(LST)
	@echo Platform cleaning done.
